package ejb;

import java.util.List;
import java.util.logging.Level;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Facade for entity Usuario.
 * 
 * @see ejb.Usuario
 * @author MyEclipse Persistence Tools
 */
@Stateless
public class UsuarioFacade implements UsuarioFacadeLocal, UsuarioFacadeRemote {
	// property constants
	public static final String NOME = "nome";
	public static final String EMAIL = "email";
	public static final String TIPO = "tipo";
	public static final String SENHA = "senha";

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Perform an initial save of a previously unsaved Usuario entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method.
	 * 
	 * @param entity
	 *            Usuario entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Usuario entity) {
		LogUtil.log("saving Usuario instance", Level.INFO, null);
		try {
			entityManager.persist(entity);
			LogUtil.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Usuario entity.
	 * 
	 * @param entity
	 *            Usuario entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Usuario entity) {
		LogUtil.log("deleting Usuario instance", Level.INFO, null);
		try {
			entity = entityManager.getReference(Usuario.class, entity
					.getUsuario());
			entityManager.remove(entity);
			LogUtil.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Usuario entity and return it or a copy of it
	 * to the sender. A copy of the Usuario entity parameter is returned when
	 * the JPA persistence mechanism has not previously been tracking the
	 * updated entity.
	 * 
	 * @param entity
	 *            Usuario entity to update
	 * @return Usuario the persisted Usuario entity instance, may not be the
	 *         same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Usuario update(Usuario entity) {
		LogUtil.log("updating Usuario instance", Level.INFO, null);
		try {
			Usuario result = entityManager.merge(entity);
			LogUtil.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			LogUtil.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Usuario findById(String id) {
		LogUtil
				.log("finding Usuario instance with id: " + id, Level.INFO,
						null);
		try {
			Usuario instance = entityManager.find(Usuario.class, id);
			return instance;
		} catch (RuntimeException re) {
			LogUtil.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all Usuario entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Usuario property to query
	 * @param value
	 *            the property value to match
	 * @return List<Usuario> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Usuario> findByProperty(String propertyName, final Object value) {
		LogUtil.log("finding Usuario instance with property: " + propertyName
				+ ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from Usuario model where model."
					+ propertyName + "= :propertyValue";
			Query query = entityManager.createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find by property name failed", Level.SEVERE, re);
			throw re;
		}
	}

	public List<Usuario> findByNome(Object nome) {
		return findByProperty(NOME, nome);
	}

	public List<Usuario> findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List<Usuario> findByTipo(Object tipo) {
		return findByProperty(TIPO, tipo);
	}

	public List<Usuario> findBySenha(Object senha) {
		return findByProperty(SENHA, senha);
	}

	/**
	 * Find all Usuario entities.
	 * 
	 * @return List<Usuario> all Usuario entities
	 */
	@SuppressWarnings("unchecked")
	public List<Usuario> findAll() {
		LogUtil.log("finding all Usuario instances", Level.INFO, null);
		try {
			final String queryString = "select model from Usuario model";
			Query query = entityManager.createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}